/*
 * Copyright 2007 Jaisy Sprl.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package be.jaisy.ui.gwt.widget.sample.client.table;

import java.util.ArrayList;
import java.util.List;

import be.jaisy.ui.gwt.widget.client.sink.Sink;
import be.jaisy.ui.gwt.widget.client.viewer.IContentProvider;
import be.jaisy.ui.gwt.widget.client.viewer.Viewer;
import be.jaisy.ui.gwt.widget.client.viewer.table.ITableLabelProvider;
import be.jaisy.ui.gwt.widget.client.viewer.table.TableViewer;

/**
 * @author Stany Blanvalet <stany.blanvalet@jaisy.be>
 */
public class TableViewerSimpleSampleSink extends Sink{

	public static SinkInfo init(){
		return new SinkInfo("TableViewerSimpleSampleSink", "<p>TableViewerSimpleSampleSink desciption</p>"){

			public Sink createInstance(){
				return new TableViewerSimpleSampleSink();
			}

			public String getColor(){
				return "#bf2a2a";
			}

		};
	}

	public TableViewerSimpleSampleSink(){

		TableViewer table = new TableViewer();
		table.addHeader(0, "name", true);
		table.addHeader(1, "description", false);
		table.addFilter(0);
		table.setContentProvider(new IContentProvider(){
			public Object[] getElements(Object inputElement){
				return ((List)inputElement).toArray();
			}

			public void inputChanged(Viewer viewer, Object oldInput, Object newInput){
				// nothing to do
			}
		});

		table.setLabelProvider(new ITableLabelProvider(){
			public Object[] getRow(Object element){
				if (element instanceof SimpleTo){
					SimpleTo simpleTo = (SimpleTo)element;
					return new Object[] {simpleTo.getName(), simpleTo.getDescription()};
				}
				return null;
			}

			public String getCellStyle(Object element, int columnIndex){
				return null;
			}

			public String getRowStyle(Object element){
				if (element instanceof SimpleTo){
					SimpleTo simpleTo = (SimpleTo)element;
					if (simpleTo.getName().indexOf("?") >= 0)
						return "-red";
				}
				return null;
			}
		});

		List inputs = new ArrayList();
		inputs.add(new SimpleTo("test1", "description1"));
		inputs.add(new SimpleTo("test2?", "description2"));

		table.setInput(inputs);

		initWidget(table);
	}

	public class SimpleTo{
		private String name;

		private String description;

		public SimpleTo(String name, String description){
			this.name = name;
			this.description = description;
		}

		public String getName(){
			return this.name;
		}

		public String getDescription(){
			return this.description;
		}
	}

}
